package easamples;

import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import org.sparx.Collection;
import org.sparx.Repository;
import org.sparx.Stereotype;

public class ManageStereotypesExample implements IExample
{
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return "Add and Manage Stereotypes";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sparx.javaexample.test.IExample#getDescription()
	 */
	public String getDescription()
	{
		// return "Example illustrating how to add Stereotypes";
		return "Example illustrating how to add and delete Stereotypes.<br/><br/><b>Related APIs</b><br/><ul><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?stereotype.htm'>Stereotype API</a></li><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?repository3.htm'>Repository API</a></li></ul>";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sparx.javaexample.test.IExample#runExample(java.io.File,
	 * javax.swing.DefaultListModel)
	 */
	public void runExample( File projectFile, ExampleUI uiFrame )
	{
		Repository rep = null;
		DefaultListModel outputList = uiFrame.getOutputListModel();
		
		try
		{
			// Create a repository object - This will create a new instance of EA
			rep = new Repository();
			
			// Attempt to open the provided file
			if ( rep.OpenFile(projectFile.getAbsolutePath()) )
			{
				// Create a new stereotype in the repository
				Collection<Stereotype> stereotypes = rep.GetStereotypes();
				String testStereoName = "TestStereotype";
				Stereotype testStereo = stereotypes.AddNew( testStereoName, "Class" );
				testStereo.Update();
				stereotypes.Refresh();
				
				
				outputList.addElement( "Added Stereotype: " + testStereoName );
				
				testStereo = null;
								
				// Navigate the stereotype collection. Delete the newly added element when we 
				// find it
				for ( short i = 0 ; i < stereotypes.GetCount() ; i++ )
				{
					Stereotype currentStereo = stereotypes.GetAt( i );
					String currentStereoName= currentStereo.GetName();
					outputList.addElement( "Stereotype: " + currentStereoName );
					
					// If the stereotype is the one we just added, then delete it
					if ( currentStereoName.equals(testStereoName) )
					{
						stereotypes.DeleteAt( i, false );
						outputList.addElement( "Deleted Stereotype: " + currentStereoName );
					}
				}
				
				outputList.addElement( "Done!" );
				
			}
			else
			{
				// If the file couldn't be opened then notify the user
				String message = "EA was unable to open the file '";
				message += projectFile.getAbsolutePath();
				message += "'.";
				JOptionPane.showMessageDialog( uiFrame, message, "Invalid File", JOptionPane.ERROR_MESSAGE );
				outputList.addElement( message );
			}
		}
		catch ( UnsatisfiedLinkError ule )
		{
			String message = "Unable to communicate with EA.\nPlease ensure that the path to SSJavaCOM.dll included in java.library.path";
			JOptionPane.showMessageDialog( uiFrame, message, "Unable to link to SSJavaCOM.dll", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		catch ( Exception e )
		{
			String message = "An unhandled exception was thrown: " + e.getMessage();
			JOptionPane.showMessageDialog( uiFrame, message, "Unhandled Exception", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		finally
		{
			if ( rep != null )
			{
				// Clean up
				rep.CloseFile();
				rep.Exit();
				rep.destroy();
			}
		}
	}

}
